﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using XNA_Game_Engine.Common;
using XNA_Game_Engine.Sounds;
using XNA_Game_Engine.GUI;

namespace XNA_Game_Engine.Events
{
    class Arrow
    {
        private Texture2D ArrowSprite;
        private Texture2D ArrowExplosion;
        private float posY;
        private float posX;
        private int angle = 27;
        private int speed = 50;
        private float mapX;
        private float mapY;
        private bool started = false;
        private bool colided = false;       
        float tx, ty;
        int imgSize;
        float angGiro = 0.0f;
        float scale = 0.0f;
        float timer = 0;

        public Arrow(Texture2D sprite, Texture2D arrowExplos)
        {
            this.ArrowSprite = sprite;
            this.ArrowExplosion = arrowExplos;
            mapX = (GameProperties.ScreenWidth / 2.0f) / 2.0f;
            mapY = GameProperties.ScreenHeight / 2.0f;
            posY = 10;
            imgSize = arrowExplos.Width;
        }

        public void Update(float gameTime)
        {

            if (!StaticGui.gameStarted) // Restarting Variables
            {
                posY = 10;
                started = false;
                
            }

            if (started) // Move Arrow
            {
                double velx = Math.Cos(MathHelper.ToRadians(angle)) * speed;
                double vely = Math.Sin(MathHelper.ToRadians(angle)) * speed;
                
                posY -= (float)vely * gameTime / 1000;
            }


            effectsControl();
            
        }

        public void Render(SpriteBatch renderTarget)
        {       
            if (started)
            {
                tx = (float)((posX * 65 / 2) + (posY * 65 / 2) + mapX) + 9 + 5;
                ty = (float)((posY * (42 - 9) / 2) - (posX * (42 - 9) / 2) + mapY) - 10 / 2;

                Rectangle rect = new Rectangle(0, 0, ArrowSprite.Width, ArrowSprite.Height);
                
                renderTarget.Draw(ArrowSprite, new Vector2(tx, ty), rect, Color.White);
            }

            if (colided)
            {
                Rectangle rect = new Rectangle(0, 0, imgSize, imgSize);
               
                renderTarget.Draw(ArrowExplosion, 
                    new Vector2(tx, ty), 
                    rect,
                    Color.White,
                    angGiro,
                    new Vector2(imgSize/2, imgSize/2), 
                    scale, 
                    SpriteEffects.None, 
                    0);
            }
        }
        
        private void effectsControl()
        {
            if (colided)
            {
                timer++;
                if (timer <= 20) // Explosion Effects Control
                {
                    angGiro = angGiro + 0.1f;
                    if (scale <= 0.5)
                        scale = scale + 0.08f;
                }
                else
                {
                    timer = 0;
                    angGiro = 0.0f;
                    scale = 0.0f;
                    colided = false;
                }
            }
        }

        public void startArrow(int plrX)
        {       
            posX = (float)plrX;
            //posY = 10;
            started = true;
        }

        public bool checkColision(int plrX, int plrY)
        {           
            //Console.WriteLine("Player X: " + plrX);
            //Console.WriteLine("Player Y: " + plrY);

            if ((this.posX == plrX) && (this.posY > plrY && this.posY < (plrY + 0.9)))
            {
               
                colided = true;
                started = false;
                posY = 10;
                SoundController.playClip("death");
                return true;
            }else
                return false;

        }

    }
}
